package myBinaryTree;

import sun.reflect.generics.tree.Tree;

import java.util.*;

public class MybinaryTree {


    public static void main(String[] args) {

    }

    static class TreeNode{
        private char val;
        private TreeNode left;
        private TreeNode right;

        public TreeNode(char val){
            this.val = val;
        }
    }

    public TreeNode create(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;
    }
    //先序遍历
    public List<TreeNode> preOrder(TreeNode root){
        List<TreeNode> ret = new LinkedList<>();
        if(root == null) return ret;
        ret.add(root);
        //将所有左子树全部放在一个List集合中
        List<TreeNode> leftTree = preOrder(root.left);
        //利用返回值将左子树全部加到最终的List ret中，  右子树同理
        ret.addAll(leftTree);
        List<TreeNode> rightTree = preOrder(root.right);
        ret.addAll(rightTree);
        return ret;
    }
    //中序遍历
    public List<TreeNode> inOrder(TreeNode root){
        List<TreeNode> ret = new LinkedList<>();
        if (root == null) return ret;
        List<TreeNode> leftTree  = inOrder(root.left);
        ret.addAll(leftTree);
        ret.add(root);
        List<TreeNode> rightTree = inOrder(root.right);
        ret.addAll(rightTree);
        return ret;
    }
    //后序遍历
    public List<TreeNode> postOrder(TreeNode root){
        List<TreeNode> ret = new LinkedList<>();
        if (root == null) return ret;
        List<TreeNode> leftTree  = inOrder(root.left);
        ret.addAll(leftTree);
        List<TreeNode> rightTree = inOrder(root.right);
        ret.addAll(rightTree);
        ret.add(root);
        return ret;
    }
    //计算节点个数  1
    private static int size = 0;
    public int getNodeSize1(TreeNode root){
        if(root == null) return 0;
        getNodeSize1(root.left);
        size++;
        getNodeSize1(root.right);
        return size;
    }
    //计算节点个数   2
    public int getNodeSize2(TreeNode root){
        if(root == null) return 0;
        return getNodeSize2(root.left) + getNodeSize2(root.right) + 1;
    }

    //计算叶子节点的个数   1
    private static int leafSize = 0;
    public int getLeafSize1(TreeNode root){
        if(root == null) return 0;
        if(root.right == null && root.left ==null) leafSize++;
        getLeafSize1(root.right);
        getLeafSize1(root.left);
        return leafSize;
    }

    //计算叶子节点的个数   2
    public int getLeafSize2(TreeNode root){
        if(root == null) return 0;
        //此处不可再写leafSize++ 否则遍历到叶子结点时也没有返回值
        if(root.right == null && root.left ==null) return 1;
        return getLeafSize2(root.left) + getLeafSize2(root.right);
    }

    //获取第K层的节点个数
    public int getKLevelSize(TreeNode root,int k){
        //root在这里的作用的判断第k层的元素是否为null，后返回0
        if(root == null) return 0;
        if(k == 1) return 1;
        return getKLevelSize(root.left,k - 1) + getKLevelSize(root.right,k -1);
    }
    //获取树的高度
    public int getHeight(TreeNode root){
        if(root == null) return 0;
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return leftHeight > rightHeight ?
                leftHeight + 1 : rightHeight + 1;
    }
    //检测值为value的元素是否存在
    public boolean find(TreeNode root, int key){
        if(root == null) return false;
        if(root.val == key)  return true;
        if(root.val == key)  return true;
        if(find(root.left,key)) return true;
        if(find(root.right,key)) return true;
        return false;
    }
    //层序遍历
    //将队列的每一层都存入对应的链表中  变种，求树的宽度以及左视图右视图等
    public List<List<TreeNode>> levelOrder(TreeNode root){
        List<List<TreeNode>> ret = new LinkedList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if (root == null){
            return ret;
        }
        queue.offer(root);
        while(queue != null){
            List<TreeNode> tmp = new LinkedList<>();
            int size = queue.size();
            while(size-- > 0){
                TreeNode node  = queue.poll();
                tmp.add(node);
                if(node.right != null){
                    queue.offer(node.right);
                }
                if(node.left != null){
                    queue.offer(node.left);
                }
            }
            ret.add(tmp);
        }
        return ret;
    }

    //判断完全二叉树
    public boolean isCompeleteTree(TreeNode root){
        if (root == null){
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            TreeNode node = queue.poll();
            if(node != null){
                queue.offer(node.left);
                queue.offer(node.right);
            } else {
                break;
            }
        }
        while(!queue.isEmpty()){
            if(queue.peek() == null){
                queue.poll();
            } else {
                return false;
            }
        }
        return true;
    }
}
